Mapping Rules
This document explains how to create mappings for the Web3 Adapter system, which enables data exchange between different platforms using the universal ontology.
Basic Structure
A mapping file defines how local database fields map to global ontology fields. The structure is:
{
"tableName": "local_table_name",
"schemaId": "global_schema_w3id",
"ownerEnamePath": "path_to_owner_ename",
"ownedJunctionTables": ["junction_table1", "junction_table2"],
"localToUniversalMap": {
"localField": "globalField",
"localRelation": "tableName(relationPath),globalAlias"
}
}
Field Mapping
Direct Field Mapping
"localField": "globalField"
Maps a local field directly to a global field with the same name.
Relation Mapping
"localRelation": "tableName(relationPath),globalAlias"
Maps a local relation to a global field, where:
tableNameis the referenced table namerelationPathis the path to the relation dataglobalAliasis the target global field name
Array Relation Mapping
"participants": "users(participants[].id),participantIds"
Maps an array of relations:
participants[].idextracts theidfield from each item in theparticipantsarrayusers()resolves each ID to a global user referenceparticipantIdsis the target global field name
Special Functions
Date Conversion (__date)
Converts various timestamp formats to ISO string format.
"createdAt": "__date(createdAt)"
"timestamp": "__date(calc(timestamp * 1000))"
Supported input formats:
- Unix timestamp (number)
- Firebase v8 timestamp (
{_seconds: number}) - Firebase v9+ timestamp (
{seconds: number}) - Firebase Timestamp objects
- Date objects
- UTC strings
Calculation (__calc)
Performs mathematical calculations using field values.
"total": "__calc(quantity * price)"
"average": "__calc((score1 + score2 + score3) / 3)"
Features:
- Supports basic arithmetic operations (+, -, *, /, etc.)
- Can reference other fields in the same entity
- Automatically resolves field values before calculation
Owner Path
The ownerEnamePath defines how to determine which eVault owns the data (via the owner's eName):
"ownerEnamePath": "ename" // Direct field
"ownerEnamePath": "users(createdBy.ename)" // Nested via relation
"ownerEnamePath": "users(participants[].ename)" // Array relation
Junction Tables
Junction tables (many-to-many relationships) can be marked as owned:
"ownedJunctionTables": [
"user_followers",
"user_following"
]
When junction table data changes, it triggers updates to the parent entity.
Examples
User Mapping
{
"tableName": "users",
"schemaId": "550e8400-e29b-41d4-a716-446655440000",
"ownerEnamePath": "ename",
"ownedJunctionTables": ["user_followers", "user_following"],
"localToUniversalMap": {
"handle": "username",
"name": "displayName",
"description": "bio",
"avatarUrl": "avatarUrl",
"ename": "ename",
"followers": "followers",
"following": "following"
}
}
Group with Relations
{
"tableName": "groups",
"schemaId": "550e8400-e29b-41d4-a716-446655440003",
"ownerEnamePath": "users(participants[].ename)",
"localToUniversalMap": {
"name": "name",
"description": "description",
"owner": "owner",
"admins": "users(admins),admins",
"participants": "users(participants[].id),participantIds",
"createdAt": "__date(createdAt)",
"updatedAt": "__date(updatedAt)"
}
}
Best Practices
- Use descriptive global field names that match the ontology schema
- Handle timestamps consistently using
__date()function - Map relations properly using the
tableName(relationPath)syntax - Use aliases when the global field name differs from the local field
- Test mappings with sample data to ensure proper conversion
- Document complex mappings with comments explaining the logic
Troubleshooting
Common Issues
- Missing relations: Ensure the referenced table has a mapping
- Invalid paths: Check that the relation path matches your entity structure
- Type mismatches: Use
__date()for timestamps,__calc()for calculations - Circular references: Avoid mapping entities that reference each other infinitely
Debug Tips
- Check the console for mapping errors
- Verify that all referenced tables have mappings
- Test with simple data first, then add complexity
- Use the
__calc()function to debug field values
References
- Web3 Adapter — Bridge between platform DB and eVault
- Ontology — Schema registry and schemaIds
- Webhook Controller — Using mappings for inbound webhooks